<?php
namespace BoxEdu\Controller\Home;
//use Think\Controller;

class SchoolsController extends CommonController
{

    public function _initialize()
    {
        parent::_initialize();
        $this->nav_cur = "Schools";
    }


    public function get_ajax_school_sel()
    {
        $model = BoxModel('EduSchools');
        if ($this->province_id = I('get.province_id')) {
            $where = " province_id='$this->province_id' and is_show=1";
        }
        if ($this->subjectid = I('get.subjectid')) {
            $ids = $this->getIdsBySubjectid($this->subjectid);
            $schools_ids = $ids['schools_ids'];
            $where .= " and id in ($schools_ids)";
        }
        $this->schools_list = $model->get($where, "", "name");
        $this->display();
    }

    /**********在线预约***/
    public function act_book()
    {
        // $this->classroom_id = I('classroom_id');
        //$this->book_date = I('book_date');
        //$this->book_week = I('book_week');
        $this->part_time_id = I('part_time_id', "0", "intval");
        $this->seat_num = I('seat_num');
        $model = BoxModel('EduClassrooms');
        if (!$this->user_id) {
            $data = $this->return_array(2, "请先登录");
            echo json_encode($data);
            exit();
        }
        if (!$this->seat_num || !$this->part_time_id) {
            $data = $this->return_array(3, "请先选择座位");
            echo json_encode($data);
            exit();
        }
								
								
        //dump($this->classrooms_info);
        /*if(strtotime($this->book_date) < strtotime(date("Y-m-d"))){
            $data=$this->return_array(3,"当前日期不可预约");
            echo json_encode($data);
            exit();
        }*/

        $part_time_info = BoxModel("edu_classrooms_subject")->load($this->part_time_id);
        $this->classroom_id = $part_time_info['class_room_id'];
        $this->classrooms_info = $model->load($this->classroom_id);
        $subjectid = $part_time_info['subjectid'];
       //是否开通线下课程
						 $has_offline=Logic("BoxEdu", "Subject")->has_offline($subjectid, $this->user_id);
							if($has_offline['error']>0){
											 $data = $this->return_array(4, "你还未开通线下班级，请先开通后使用");
            echo json_encode($data);
            exit();
								}
						  $has_books = $books = BoxModel("edu_classrooms_book")->where("subject_id='$subjectid' and user_id='$this->user_id' and status=1")->count();//自己在这个课程预约了几次
        $max_books = 1;//同一课程最多可以预约多少次
        /***座位已被预定****/
        $books = BoxModel("edu_classrooms_book")->where("part_time_id='$this->part_time_id' and seat_num='$this->seat_num' and status=1")->find();
        if ($books) {
            $data = $this->return_array(1, "座位已被预定");
            echo json_encode($data);
            exit();
        }

        $this->info = $part_time_info['week_str'] . " " . $part_time_info['lable'];
        $row = array(
            'name' => $this->classrooms_info['name'],
            'classroom_id' => $this->classroom_id,
            'book_date' => $this->book_date,
            'week' => $part_time_info['week'],
            'part_time_id' => $this->part_time_id,
            'seat_num' => $this->seat_num,
            'subject_id' => $part_time_info['subjectid'],
            'offline_subject_id' => $part_time_info['offline_subject_id'],
            'school_id' => $part_time_info['school_id'],
            'province_id' => $part_time_info['province_id'],
            'info' => $this->info,
            'add_time' => date("Y-m-d H:i:s"),
            'user_id' => $this->user_id,
        );
        $books = BoxModel("edu_classrooms_book")->where("part_time_id='$this->part_time_id' and user_id='$this->user_id' and status=1")->find();
        /*****预约总次数***/
        if ((!($max_books > $has_books)) && (!$books)) {
            $data = $this->return_array(3, "预约次数超过开课次数");
            echo json_encode($data);
            exit();
        }

        if ($books) {
            $r = BoxModel("edu_classrooms_book")->where(array('id' => $books['id']))->save($row);
        } else {
            $r = BoxModel("edu_classrooms_book")->add($row);
        }
        //		dump(BoxModel("edu_classrooms_book"));
        if ($r !== false) {
            $data = $this->return_array(0, "选座成功");
        } else {
            $data = $this->return_array(2, "信息保存失败，请重试");
        }
        echo json_encode($data);
        exit();

    }


}